package com.avasthi.cli.twitter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;

import javax.net.ssl.HttpsURLConnection;

import de.l3s.boilerpipe.BoilerpipeProcessingException;
import de.l3s.boilerpipe.extractors.ArticleExtractor;
import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import net.minidev.json.JSONValue;
import net.minidev.json.parser.JSONParser;
import net.minidev.json.parser.ParseException;

public class TwitterJsonRetweetFlow {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		if (args.length == 0) {
			System.out.println("Usage: java TwitterJsonParser twitterlog");
			System.exit(0);
		}
		File inp = new File(args[0]);
		System.out.println("Reading file :" + inp.getAbsolutePath());
		Map<String, TweetTrace> tweetTraceMap = new HashMap<String, TweetTrace>();
		try {
			SimpleDateFormat df = new SimpleDateFormat(
					"EEE MMM d HH:mm:ss Z yyyy");
			FileReader fr = new FileReader(inp);
			BufferedReader reader = new BufferedReader(fr);
			JSONParser parser = new JSONParser(JSONParser.MODE_PERMISSIVE);
			JSONObject jv = null;
			Object o = null;
			while ((o = parser.parse(reader)) != null) {
				if (o instanceof java.lang.String) {
					System.out.println((String) o);
					break;
				}
				jv = (JSONObject) o;
				Long id = (Long) jv.get("id");
				JSONObject retweeted_status = (JSONObject) jv
						.get("retweeted_status");
				if (retweeted_status != null) {
					String originalId = (String) retweeted_status.get("id_str");
					TweetTrace tt = tweetTraceMap.get(originalId);
					if (tt == null) {
						tt = new TweetTrace(originalId);
						tt.text = (String) retweeted_status.get("text");
						try {
							tt.timestamp = df.parse((String) retweeted_status
									.get("created_at"));
						} catch (java.text.ParseException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						tt.userid = (String) ((JSONObject) retweeted_status
								.get("user")).get("id_str");
						tweetTraceMap.put(originalId, tt);
					}
					try {
						RetweetTrace rt = new RetweetTrace(
								(String) jv.get("id_str"),
								(String) ((JSONObject) jv.get("user"))
										.get("id_str"), df.parse((String) jv
										.get("created_at")));
						populateMetaDataInRetweet(retweeted_status, rt);
						tt.addRetweet(rt);
					} catch (java.text.ParseException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					// Find the original tweet

				} else {

					String originalId = (String) jv.get("id_str");
					TweetTrace tt = new TweetTrace(originalId);
					tt.text = (String) jv.get("text");
					try {
						String dateStr = (String) jv.get("created_at");
						if (dateStr != null) {
							
							tt.timestamp = df.parse((String) jv.get("created_at"));
						}
					} catch (java.text.ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					tt.userid = (String) ((JSONObject) jv.get("user"))
							.get("id_str");
					tweetTraceMap.put(originalId, tt);
					populateMetaDataInTweet(jv, tt);
				}
			}

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		File f = new File("/home/vavasthi/tweets/rtflow.csv");
		PrintWriter writer;
		File sf = new File("/home/vavasthi/tweets/rtflowstat.csv");
		PrintWriter statWriter;
		try {
			writer = new PrintWriter(f, "UTF-8");
			statWriter = new PrintWriter(sf, "UTF-8");
			for (TweetTrace tt : tweetTraceMap.values()) {
				tt.print(writer);
				tt.printStat(statWriter, false);
				
			}
			writer.close();
			statWriter.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println("Original Tweets found = " + tweetTraceMap.size());
	}
	static private void populateMetaDataInRetweet(JSONObject jv, RetweetTrace rt) {

		JSONObject entities = (JSONObject) jv.get("entities");
		if (entities != null) {

			JSONArray hashtags = (JSONArray) entities
					.get("hashtags");
			if (hashtags != null && hashtags.size() > 0) {
				rt.hashtags += hashtags.size();
			}
			JSONArray urls = (JSONArray) entities.get("urls");
			if (urls != null) {
				rt.urls += urls.size();
				
				for (JSONObject u : urls
						.toArray(new JSONObject[urls.size()])) {
					String su = u.get("expanded_url")
							.toString();
					/*try {
						URL url = new URL(su);
						HttpURLConnection huc = (HttpURLConnection) url.openConnection();
						huc.setFollowRedirects(true);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}*/
				}
			}							

		}
		
	}
	static private void populateMetaDataInTweet(JSONObject jv, TweetTrace tt) {

		JSONObject entities = (JSONObject) jv.get("entities");
		if (entities != null) {

			JSONArray hashtags = (JSONArray) entities
					.get("hashtags");
			if (hashtags != null && hashtags.size() > 0) {
				tt.hashtags += hashtags.size();
			}
			JSONArray urls = (JSONArray) entities.get("urls");
			if (urls != null) {
				tt.urls += urls.size();
				
				for (JSONObject u : urls
						.toArray(new JSONObject[urls.size()])) {
					String su = u.get("expanded_url")
							.toString();
/*					try {
						URL url = new URL(su);
						HttpURLConnection huc = (HttpURLConnection) url.openConnection();
						huc.setFollowRedirects(true);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}*/
				}
			}							

		}
		
	}
}
